﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using EIA.Basic.Model;
using System.Windows.Data;

namespace EIA.SLClient.ProjectManage
{
	public partial class StaffSelector : UserControl
	{
		public StaffSelector()
		{
			// 为初始化变量所必需
            InitializeComponent();
        }

        #region SelectedStaff (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public TStaff SelectedStaff
        {
            get { return (TStaff)GetValue(SelectedStaffProperty); }
            set { SetValue(SelectedStaffProperty, value); }
        }
        public static readonly DependencyProperty SelectedStaffProperty =
            DependencyProperty.Register("SelectedStaff", typeof(TStaff), typeof(StaffSelector),
            new PropertyMetadata(null, new PropertyChangedCallback(OnSelectedStaffChanged)));

        private static void OnSelectedStaffChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            StaffSelector source = (StaffSelector)sender;

            // 处理逻辑
            TStaff newValue = (TStaff)args.NewValue;
            if (source.lst_staff.SelectedItem != newValue) source.lst_staff.SelectedItem = newValue;
        }

        #endregion

        #region StaffCollection (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public IEnumerable<TStaff> StaffCollection
        {
            get { return (IEnumerable<TStaff>)GetValue(StaffCollectionProperty); }
            set { SetValue(StaffCollectionProperty, value); }
        }
        public static readonly DependencyProperty StaffCollectionProperty =
            DependencyProperty.Register("StaffCollection", typeof(IEnumerable<TStaff>), typeof(StaffSelector),
            new PropertyMetadata(null, new PropertyChangedCallback(OnStaffCollectionChanged)));

        private static void OnStaffCollectionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            StaffSelector source = (StaffSelector)sender;

            // 处理逻辑
            IEnumerable<TStaff> newValue = (IEnumerable<TStaff>)args.NewValue;
            if (newValue != null)
            {
                var select = source.SelectedStaff;
                var view = new PagedCollectionView(newValue);
                view.Filter = (o) => { return (o is TStaff) ? (o as TStaff).StfName.Contains(source.txt_filter.Text) : true; };
                source.lst_staff.ItemsSource = view;
                source.lst_staff.SelectedItem = select;
            }
            else source.lst_staff.ItemsSource = null;
        }

        #endregion


        private void txt_filter_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            if (lst_staff.ItemsSource is PagedCollectionView)
            {
                isFiltering = true;
                (lst_staff.ItemsSource as PagedCollectionView).Refresh();
                isFiltering = false;
            }
        }
        bool isFiltering = false;

        private void lst_staff_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            this.SelectedStaff = lst_staff.SelectedItem as TStaff;
            this.txt_display.Text = this.SelectedStaff == null ? "" : this.SelectedStaff.StfName;
            if(!isFiltering)this.popup.IsOpen = false;
        }
	}
}